package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;


public class customer_menu_option {
    list_book list_book = new list_book();
    checkout checkout = new checkout();
    return_book return_book = new return_book();
    checkout_movie checkoutMovie = new checkout_movie();
    list_movie listMovie = new list_movie();
    userinfor userinfor = new userinfor();

    public boolean check_option_valid(ArrayList<String> menu_list,String customer_option){
        for (int i = 0; i < menu_list.size(); i++){
            if ( menu_list.get(i).equals(customer_option))
                return true;

        }
        return false;
    }

    public void  menu_option(String customer_option) throws IOException {
        if (customer_option.equals("List books")){
            list_book.list_avalliable_book();
        }
        else if (customer_option.equals("List movies")){
            listMovie.show_list(listMovie.list_avalliable_movie(listMovie.get_movie_list()));
        }
        else if (customer_option.equals("List books with holder")){
            Login_and_list_books_with_holder();
        }
        else if (customer_option.equals("checkout book")){
            Loglin_and_checkout_book();
        }
        else if (customer_option.equals("return book")){
            Login_and_return_book();
        }
        else if (customer_option.equals("checkout movie")) {
            checkout_movie();
        }
        else if (customer_option.equals("userinfor")) {
            show_userinfor();
        }
    }

    private void checkout_movie() throws IOException {
        boolean con = true;
        do {
            System.out.println("plealse enter the name or Quit ");
            String movie_name = get_user_enter();
            if (movie_name.equals("Quit"))
                con = false;
            else {
                checkoutMovie.checkout_movie(movie_name, listMovie.get_movie_list());
            }
        } while (con);
    }

    private void Login_and_return_book() throws IOException {
        System.out.println("plealse enter your  library number");
        String usernum = get_user_enter();
        System.out.println("plealse enter your  password");
        String password = get_user_enter();
        if (userinfor.check_user_vaild(usernum,password)) {
            boolean con = true;
            do {
                System.out.println("plealse enter the name or Quit ");
                String book_name = get_user_enter();
                if (book_name.equals("Quit"))
                    con = false;
                else {
                    String user_name = find_book_holder(usernum);
                    return_book.return_book(user_name,book_name, list_book.get_book_list());
                }
            } while (con);
        }
    }

    private void Loglin_and_checkout_book() throws IOException {
        System.out.println("plealse enter your  library number");
        String usernum = get_user_enter();
        System.out.println("plealse enter your  password");
        String password = get_user_enter();

        if (userinfor.check_user_vaild(usernum,password)) {
            boolean con = true;
            do {
                System.out.println("plealse enter the name or Quit ");
                String book_name = get_user_enter();
                if (book_name.equals("Quit"))
                    con = false;
                else {
                    String user_name = find_book_holder(usernum);
                    checkout.checkout(user_name,book_name, list_book.get_book_list());
                }
            } while (con);
        }
    }
    private void show_userinfor() throws IOException {
        System.out.println("plealse enter your  library number");
        String usernum = get_user_enter();
        System.out.println("plealse enter your  password");
        String password = get_user_enter();

        if (userinfor.check_user_vaild(usernum,password))
            userinfor.show_userinfor(usernum);
    }

    private void Login_and_list_books_with_holder() throws IOException {
        System.out.println("plealse enter your  library number");
        String usernum = get_user_enter();
        System.out.println("plealse enter your  password");
        String password = get_user_enter();
        if (userinfor.check_user_vaild(usernum,password))
        list_book.list_book_with_holder();
    }

    public String find_book_holder(String num){
        for (int i = 0; i<userinfor.users.size(); i++){
            if (userinfor.users.get(i).getUser_num().equals(num))
                return userinfor.users.get(i).getName();
        }
        return "sorry";
    }
    public String get_user_enter() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        return br.readLine();
    }
}
